package com.lance.answer.gui.answer.gk.answer.lms.batch;

import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.lance.answer.gui.answer.AnswerObject;
import com.lance.answer.gui.answer.DealJsonString;
import com.lance.answer.gui.answer.Util;
import com.lance.answer.gui.answer.gk.answer.lms.batch.dto.CourseConfig;
import com.lance.answer.gui.answer.gk.answer.lms.batch.dto.LmsWatchDTO;
import com.lance.answer.gui.answer.gk.answer.lms.batch.task.DoAnswerTask;
import com.lance.answer.gui.answer.gk.answer.lms.batch.task.LmsSaveVideoTask;
import com.lance.answer.gui.answer.gk.answer.lms.util.LmsUtil;
import org.apache.commons.io.IOUtils;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.util.*;
import java.util.concurrent.LinkedBlockingDeque;

public class DoAnswer {


    public static void main(String[] args) throws Exception {



        int sync = 8;

        LinkedBlockingDeque<LmsWatchDTO> watchQueue = new LinkedBlockingDeque<>();

        String courseConfigStr = IOUtils.toString(new FileInputStream("libs//config//courseConf.json"), "UTF-8");
        List<CourseConfig> CourseConfigList = JSONObject.parseArray(courseConfigStr, CourseConfig.class);
        Map<String,CourseConfig> courseConfigMap = Maps.newHashMap();
        CourseConfigList.forEach(courseConfig -> courseConfigMap.put(courseConfig.getCourseName(),courseConfig));


        List<String> teachers = Lists.newArrayList();
        teachers.add("刘庆");
        teachers.add("李俊杰");
        teachers.add("阿迪力");

        Map<String,Integer> courseCount = Maps.newHashMap();
        Map<String,Integer> sortCourseCount = Maps.newLinkedHashMap();

        for (String teacher : teachers) {
            InputStream inputStream2 = new FileInputStream("libs//2022秋//teacher//" + teacher + ".user");
            List<String> userString = IOUtils.readLines(inputStream2, "utf-8");

            for (String user : userString) {
                String[] strs = user.split(",");
                String userCode = strs[0];
                String pwd = strs[1];
                LmsWatchDTO lmsWatchDTO = new LmsWatchDTO();
                lmsWatchDTO.setUsername(userCode);
                lmsWatchDTO.setPassword(pwd);

                Map<Long, String> courseMap = LmsUtil.getUserCourseMap(userCode,teacher);
                if (null == courseMap) {
                    continue;
                }

                Map<Long, Map<String, AnswerObject>> answers = Maps.newHashMap();
                for (Long courseId : courseMap.keySet()) {
                    Integer count = courseCount.get(courseMap.get(courseId));
                    if(count ==null){
                        count = 0;
                    }
                    courseCount.put(courseMap.get(courseId),++count);
                    Map<String, AnswerObject> answerMap = initAnswer(courseMap.get(courseId));
                    answers.put(courseId, answerMap);
                }
                lmsWatchDTO.setCourseMap(courseMap);
                lmsWatchDTO.setAnswers(answers);
                lmsWatchDTO.setTeacher(teacher);
                lmsWatchDTO.setCourseConfigMap(courseConfigMap);
                watchQueue.add(lmsWatchDTO);

            }


            List<Map.Entry<String, Integer>> sortList = new ArrayList<>(courseCount.entrySet());
            sortList.sort(Comparator.comparingInt(Map.Entry::getValue));
            for (Map.Entry<String, Integer> entry : sortList) {
                sortCourseCount.put(entry.getKey(), entry.getValue());
            }
            for(Map.Entry<String, Integer> entry:sortCourseCount.entrySet()){
                System.err.println("subject:"+entry.getKey()+"  :"+entry.getValue());
            }
       //     Thread.sleep(20000000);

        }
        for (int i = 1; i <= sync; i++) {
            Thread answerThread = new DoAnswerTask("d:\\ynouAutoStudy\\chromedriver.exe", watchQueue);
            //log.info("加载课程ID：{} 名称：{} ", openCourseId,subjectName);
            answerThread.setName("答题线程--" + i);
            answerThread.start();
        }


    }


    private static Map<String, AnswerObject> initAnswer(String subjectName) throws IOException {

        List<String> filterCourses = Lists.newArrayList();
        filterCourses.add("语文（2）");
        filterCourses.add("语文（1）");
        filterCourses.add("法理学");
        filterCourses.add("民法学(1)");
        filterCourses.add("药物化学");  // 没有形考任务
        filterCourses.add("医药商品营销实务");  // 没有形考任务
        filterCourses.add("资源与运营管理");  // 没有形考任务
        filterCourses.add("");
        if (
                !subjectName.contains("习近平新时代中国特色社会主义思想")
                        && !subjectName.contains("思想道德修养与法律基础")
                        && !subjectName.contains("思想道德与法治")
                        && !subjectName.contains("毛泽东思想和中国特色社会主义理论体系概论")
                        && !subjectName.contains("马克思主义基本原理概论")
                        && !subjectName.contains("国家开放大学学习指南")
                        && !subjectName.contains("计算机应用基础")
                        && !subjectName.contains("中国近现代史纲要")
                        && !subjectName.contains("英语")
                        && !subjectName.contains("经济法学")
                        && !subjectName.contains("行政组织学")
                        && !subjectName.contains("办公室管理")
                        && !subjectName.contains("个人与团队管理")
                        && !subjectName.contains("社会调查研究与方法")
                       // && !subjectName.contains("公务员制度讲座")
                      //  && !subjectName.contains("社区治理")
                        && !subjectName.contains("应用写作（汉语）")
                        && !subjectName.contains("社会心理适应")
               //         && !subjectName.contains("药物化学") // 没有形考任务
                        && !subjectName.contains("药理学（药）")
                        && !subjectName.contains("中医药学概论") //TODO
                 //       && !subjectName.contains("药剂学") //TODO 没有形考 但是有上传文件的形考1
                        && !subjectName.contains("调剂学") // 可以获取一下答案
                        && !subjectName.contains("药物治疗学")
                        && !subjectName.contains("医药商品营销实务")
                     //   && !subjectName.contains("电子商务概论") //TODO
                   //     && !subjectName.contains("资源与运营管理")  //没有形考
                        && !subjectName.contains("市场营销学")
        ) {
            return null;
        }

        File file = new File("D:\\project\\answer-tool\\src\\main\\resources\\" + subjectName + "\\" + subjectName + "-answer.json");
        if (!file.exists()) {
            return null;
        }
        InputStream inputStream = new FileInputStream(file);
        String answerStr = IOUtils.toString(inputStream, StandardCharsets.UTF_8);
        List<AnswerObject> list = JSONObject.parseArray(answerStr, AnswerObject.class);
        Map<String, AnswerObject> answerMap = Maps.newHashMap();

        boolean isEngCourse;
        isEngCourse = subjectName.contains("英语");
        for (AnswerObject answerObject : list) {
            answerMap.put(answerObject.getQuestionTitle(), answerObject);
            if (isEngCourse) {
                answerMap.put(DealJsonString.removeSpace(answerObject.getQuestionTitle()), answerObject);
            } else {
                answerMap.put(DealJsonString.saveZhWord(answerObject.getQuestionTitle()), answerObject);
                answerMap.put(Util.dealContent(answerObject.getQuestionTitle()), answerObject);
                answerMap.put(DealJsonString.delHTMLTag(Util.dealContent(answerObject.getQuestionTitle())), answerObject);
            }
        }

        return answerMap;
    }

}
